package com.or.cloud.view.activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Toast;

import com.blankj.utilcode.util.ToastUtils;
import com.luck.picture.lib.entity.LocalMedia;
import com.or.cloud.R;
import com.or.cloud.databinding.ActivityMediaClipBinding;
import com.or.common.CMConstants;
import com.or.common.base.CMBaseActivity;
import com.or.common.base.CMBaseViewModel;
import com.or.common.utils.CMFileUtils;
import com.or.ffmpeg.FFmpegUtils;

import java.util.ArrayList;

public class MediaClipActivity extends CMBaseActivity<ActivityMediaClipBinding, CMBaseViewModel> {
    String SDPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + "/video/output.mp4";

    ArrayList<LocalMedia> result;

    public static void start(Context context, ArrayList<LocalMedia> result) {
        Intent intent = new Intent(context, MediaClipActivity.class);
        intent.putExtra(CMConstants.INTENT_BEAN, result);
        context.startActivity(intent);
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_media_clip;
    }

    @Override
    protected void initView() {
        this.result = (ArrayList<LocalMedia>) getIntent().getSerializableExtra(CMConstants.INTENT_BEAN);
        binding.ctbTitle.setCenterTitle("视频剪辑").setRightTitle("确定");

        if (this.result != null && !this.result.isEmpty()) {
            String fileInfo = "文件名称:" + result.get(0).getFileName() + "\n" +
                    "文件大小:" + CMFileUtils.formatFileSize(result.get(0).getSize()) + "\n" +
                    "文件地址:" + result.get(0).getPath() + "\n";
            binding.tvMediaInfo.setText(fileInfo);

            binding.vvPlayer.setVideoURI(Uri.parse(result.get(0).getRealPath()));
        }
    }

    @Override
    protected void initListener() {
        binding.btnPlay.setOnClickListener(v -> binding.vvPlayer.start());
        binding.btnPause.setOnClickListener(v -> binding.vvPlayer.pause());
        binding.btnOut.setOnClickListener(v -> {
            binding.vvPlayer.setVideoURI(Uri.parse(SDPath));
            binding.vvPlayer.start();
        });
        binding.btnClip.setOnClickListener(v -> {
            String inputPath = result.get(0).getRealPath();
//            String outputPath = SDPath + "output.mp4";
            int startMs = 1000; // 10 seconds
            int endMs = 3000; // 20 seconds
            FFmpegUtils.cutVideo(inputPath, SDPath, startMs, endMs);
            ToastUtils.showShort("成功");
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


}
